package com.example.passwordmanager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.passwordmanager.Util.TOTP;
import com.example.passwordmanager.databinding.ActivityGet2faBinding;
import com.example.passwordmanager.databinding.ActivityShowPsdBinding;
import com.example.util.base_util.ClipboardUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class Get2FACodeActivity extends BaseActivity {

    public static void start(Context context, String title, String secretKey) {
        Intent starter = new Intent(context, Get2FACodeActivity.class);
        starter.putExtra("title", title);
        starter.putExtra("secretKey", secretKey);
        context.startActivity(starter);
    }

    private ActivityGet2faBinding binding;
    private String title;
    private String secretKey;


    Get2FACodeActivityHandler handler = new Get2FACodeActivityHandler(this);

    private static class Get2FACodeActivityHandler extends Handler {


        private WeakReference<Get2FACodeActivity> mWeakReference;

        public Get2FACodeActivityHandler(Get2FACodeActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            Get2FACodeActivity activity = mWeakReference.get();
            if (activity != null) {
                activity.handler.removeMessages(0);
                activity.changeData();
                activity.handler.sendEmptyMessage(0);

            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGet2faBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        title = getIntent().getStringExtra("title");
        secretKey = getIntent().getStringExtra("secretKey");
        if (TextUtils.isEmpty(secretKey)) {
            finish();
            return;
        }
        binding.tvTitle.setText(title);
        handler.sendEmptyMessage(0);
        binding.tvCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = binding.tvCode.getText().toString();
                ClipboardUtils.copyText(s, Get2FACodeActivity.this);
                Toast.makeText(Get2FACodeActivity.this, "已复制", Toast.LENGTH_SHORT).show();
            }
        });
    }



    void changeData(){
        long l = System.currentTimeMillis()/1000;
        try {
            int totp = TOTP.generateTOTP(secretKey, l);
            binding.tvCode.setText(getSixCode(totp));
        }catch (Exception e){
            e.printStackTrace();
            binding.tvCode.setText("生成失败:"+e.toString());
        }
        int time= (int) (30-(l%30));
        binding.tvTime.setText(time+"秒后刷新");
    }

    /**
     * 获取6位code  避免前面为0的时候不显示
     * @param toptp
     * @return
     */
    String getSixCode(int toptp){
        List<String> list=new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            int num=toptp%10;
            toptp=toptp/10;
            list.add(0,num+"");
        }
        StringBuilder stringBuffer=new StringBuilder();
        for (String s : list) {
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(0);
    }
}
